using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace ICP_MS.Plugin.Experiment;

public class FormSeqConfig : Form
{
	private SeqExperiment seqExperiment;

	private List<ExperimentDatasSeq> experimentSeqInFormAll;

	private IContainer components = null;

	private GroupBox groupBox1;

	private DataGridView dataGridView1;

	private DataGridViewTextBoxColumn ColSeq;

	private DataGridViewTextBoxColumn ColConfig;

	private CheckBox chkAutoSampler;

	public FormSeqConfig()
	{
		InitializeComponent();
	}

	public void SetParam(List<ExperimentDatasSeq> experimentSeqInForm, SeqExperiment experiment)
	{
		seqExperiment = experiment;
		experimentSeqInFormAll = experimentSeqInForm;
	}

	private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
	{
		if (e.Button != MouseButtons.Right)
		{
			return;
		}
		ContextMenu menu = new ContextMenu();
		menu.MenuItems.Add(Lang.Txt("AddFormSeqConfig.Add"), delegate
		{
			seqExperiment.DatasSeqs.Add(new ExperimentDatasSeq("", null));
			BindDatas();
		});
		menu.MenuItems.Add(Lang.Txt("AddFormSeqConfig.Delete"), delegate
		{
			if (dataGridView1.SelectedRows.Count > 0)
			{
				seqExperiment.DatasSeqs.RemoveAt(seqExperiment.DatasSeqs.FindIndex((ExperimentDatasSeq r) => r.Name == dataGridView1.SelectedRows[0].Cells[1].Value.ToString()));
				BindDatas();
			}
		});
		menu.Show(dataGridView1, e.Location);
	}

	private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
	{
		if (e.ColumnIndex == 0)
		{
			dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = e.RowIndex + 1;
		}
	}

	private void FormSeqConfig_Load(object sender, EventArgs e)
	{
		ApplyResources();
		BindDatas();
	}

	private void ApplyResources()
	{
		groupBox1.Text = Lang.Txt("SeaExperiment.MethodList.Text");
		ColSeq.HeaderText = Lang.Txt("SeaExperiment.MethodList.FormSeqConfig.Sequence");
		ColConfig.HeaderText = Lang.Txt("SeaExperiment.MethodList.FormSeqConfig.Method");
		chkAutoSampler.Text = Lang.Txt("SeaExperiment.MethodList.FormSeqConfig.chkAutoSampler");
	}

	private void BindDatas()
	{
		dataGridView1.AutoGenerateColumns = false;
		if (dataGridView1.DataSource == null)
		{
			dataGridView1.DataSource = new BindingList<ExperimentDatasSeq>(seqExperiment.DatasSeqs);
			return;
		}
		BindingContext[dataGridView1.DataSource].SuspendBinding();
		dataGridView1.DataSource = null;
		dataGridView1.DataSource = new BindingList<ExperimentDatasSeq>(seqExperiment.DatasSeqs);
		BindingContext[dataGridView1.DataSource].ResumeBinding();
	}

	private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
	{
		if (e.RowIndex < 0 || e.ColumnIndex != 1)
		{
			return;
		}
		using FormSeqConfigSelect form = new FormSeqConfigSelect();
		form.SetParams(experimentSeqInFormAll);
		if (form.ShowDialog() == DialogResult.OK && form.experimentDatasSeqSelected != null)
		{
			seqExperiment.DatasSeqs[e.RowIndex] = form.experimentDatasSeqSelected;
			BindDatas();
		}
	}

	private void chkAutoSampler_CheckedChanged(object sender, EventArgs e)
	{
		seqExperiment.autoSample = chkAutoSampler.Checked;
		seqExperiment.OnSamplerChanged();
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
		this.groupBox1 = new System.Windows.Forms.GroupBox();
		this.dataGridView1 = new System.Windows.Forms.DataGridView();
		this.ColSeq = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.ColConfig = new System.Windows.Forms.DataGridViewTextBoxColumn();
		this.chkAutoSampler = new System.Windows.Forms.CheckBox();
		this.groupBox1.SuspendLayout();
		((System.ComponentModel.ISupportInitialize)this.dataGridView1).BeginInit();
		base.SuspendLayout();
		this.groupBox1.Controls.Add(this.dataGridView1);
		this.groupBox1.Location = new System.Drawing.Point(12, 10);
		this.groupBox1.Name = "groupBox1";
		this.groupBox1.Size = new System.Drawing.Size(776, 424);
		this.groupBox1.TabIndex = 0;
		this.groupBox1.TabStop = false;
		this.groupBox1.Text = "应用序列";
		this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
		this.dataGridView1.Columns.AddRange(this.ColSeq, this.ColConfig);
		this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.dataGridView1.Location = new System.Drawing.Point(3, 21);
		this.dataGridView1.Name = "dataGridView1";
		this.dataGridView1.RowTemplate.Height = 27;
		this.dataGridView1.Size = new System.Drawing.Size(770, 400);
		this.dataGridView1.TabIndex = 0;
		this.dataGridView1.CellDoubleClick += new System.Windows.Forms.DataGridViewCellEventHandler(dataGridView1_CellDoubleClick);
		this.dataGridView1.CellFormatting += new System.Windows.Forms.DataGridViewCellFormattingEventHandler(dataGridView1_CellFormatting);
		this.dataGridView1.MouseClick += new System.Windows.Forms.MouseEventHandler(dataGridView1_MouseClick);
		dataGridViewCellStyle1.Format = "N0";
		dataGridViewCellStyle1.NullValue = null;
		this.ColSeq.DefaultCellStyle = dataGridViewCellStyle1;
		this.ColSeq.HeaderText = "序列";
		this.ColSeq.Name = "ColSeq";
		this.ColSeq.ReadOnly = true;
		this.ColConfig.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill;
		this.ColConfig.DataPropertyName = "Name";
		this.ColConfig.HeaderText = "方法";
		this.ColConfig.Name = "ColConfig";
		this.ColConfig.ReadOnly = true;
		this.chkAutoSampler.AutoSize = true;
		this.chkAutoSampler.Location = new System.Drawing.Point(15, 460);
		this.chkAutoSampler.Name = "chkAutoSampler";
		this.chkAutoSampler.Size = new System.Drawing.Size(89, 19);
		this.chkAutoSampler.TabIndex = 1;
		this.chkAutoSampler.Text = "自动进样";
		this.chkAutoSampler.UseVisualStyleBackColor = true;
		this.chkAutoSampler.CheckedChanged += new System.EventHandler(chkAutoSampler_CheckedChanged);
		base.AutoScaleDimensions = new System.Drawing.SizeF(8f, 15f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(864, 569);
		base.Controls.Add(this.chkAutoSampler);
		base.Controls.Add(this.groupBox1);
		base.Name = "FormSeqConfig";
		this.Text = "应用方法序列";
		base.Load += new System.EventHandler(FormSeqConfig_Load);
		this.groupBox1.ResumeLayout(false);
		((System.ComponentModel.ISupportInitialize)this.dataGridView1).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
